package com.example.demofx.master;

import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Contact {
    private final IntegerProperty id = new SimpleIntegerProperty();
    private final StringProperty name = new SimpleStringProperty();
    private final StringProperty phone = new SimpleStringProperty();
    private final StringProperty mobile = new SimpleStringProperty();
    private final StringProperty photo_path = new SimpleStringProperty();
    private final StringProperty email = new SimpleStringProperty();
    private final StringProperty company = new SimpleStringProperty();
    private final StringProperty homeAddress = new SimpleStringProperty();
    private final StringProperty postalCode = new SimpleStringProperty();
    private final StringProperty remark = new SimpleStringProperty();// 备注
    private LocalDate birthday;
    private BooleanProperty starred = new SimpleBooleanProperty(false);
    private BooleanProperty selected = new SimpleBooleanProperty(false); // 是否选中
    private final ObservableList<Group> groups = FXCollections.observableArrayList(); // 所属组列表
    private final StringProperty wechat = new SimpleStringProperty();
    private final StringProperty tiktok = new SimpleStringProperty();
    private final StringProperty red_book = new SimpleStringProperty();
    private final StringProperty weibo = new SimpleStringProperty();
    private final StringProperty QQ = new SimpleStringProperty();


    public Contact(String name,  String mobile) {
        setName(name);
        setMobile(mobile);
    }
    public Contact() {
    }
    // ID
    public int getId() {
        return id.get();
    }

    public void setId(int id) {
        this.id.set(id);
    }

    public IntegerProperty idProperty() {
        return id;
    }

    // 姓名
    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public StringProperty nameProperty() {
        return name;
    }

    // 固定电话
    public String getPhone() {
        return phone.get();
    }

    public void setPhone(String phone) {
        this.phone.set(phone);
    }

    public StringProperty phoneProperty() {
        return phone;
    }

    // 手机号码
    public String getMobile() {
        return mobile.get();
    }

    public void setMobile(String mobile) {
        this.mobile.set(mobile);
    }

    public StringProperty mobileProperty() {
        return mobile;
    }

    // 电子邮箱
    public String getEmail() {
        return email.get();
    }

    public void setEmail(String email) {
        this.email.set(email);
    }

    public StringProperty emailProperty() {
        return email;
    }

    // 工作单位
    public String getCompany() {
        return company.get();
    }

    public void setCompany(String Company) {
        this.company.set(Company);
    }

    public StringProperty companyProperty() {
        return company;
    }

    // 家庭地址
    public String getHomeAddress() {
        return homeAddress.get();
    }

    public void setHomeAddress(String homeAddress) {
        this.homeAddress.set(homeAddress);
    }

    public StringProperty homeAddressProperty() {
        return homeAddress;
    }

    // 邮政编码
    public String getPostalCode() {
        return postalCode.get();
    }

    public void setPostalCode(String postalCode) {
        this.postalCode.set(postalCode);
    }

    public StringProperty postalCodeProperty() {
        return postalCode;
    }

    // 备注
    public String getRemark() {
        return remark.get();
    }

    public void setRemark(String remark) {
        this.remark.set(remark);
    }

    public StringProperty remarkProperty() {
        return remark;
    }

    // 生日
    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    // 是否收藏
    public boolean isStarred() {
        return starred.get();
    }

    public void setStarred(boolean starred) {
        this.starred.set(starred);
    }

    public BooleanProperty starredProperty() {
        return starred;
    }

    // 所属组相关方法
    public ObservableList<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups.setAll(groups);
    }

    // 选中状态相关方法
    public boolean isSelected() {
        return selected.get();
    }

    public void setSelected(boolean selected) {
        this.selected.set(selected);
    }

    public BooleanProperty selectedProperty() {
        return selected;
    }

    public String getWechat() {
        return wechat.get();
    }

    public void setWechat(String wechat) {
        this.wechat.set(wechat);
    }

    public StringProperty wechatProperty() {
        return wechat;
    }

    public String getQQ() {
        return QQ.get();
    }

    public void setQQ(String qq) {
        this.QQ.set(qq);
    }

    public StringProperty QQProperty() {
        return QQ;
    }

    public String getPhotoPath() {
        return photo_path.get();
    }
    public void setPhotoPath(String photoPath) {
        this.photo_path.set(photoPath);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Contact contact = (Contact) o;
        return getId() == contact.getId();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId());
    }

    // 重写 toString 方法，返回组名
    @Override
    public String toString() {
        return name.get(); // 显示组名
    }

    /**
     * 从另一个联系人对象同步所有属性
     * @param other 来源联系人对象
     */
    public void updateFrom(Contact other) {
        if (other == null) return;

        // 基础信息
        this.setId(other.getId());
        this.setName(other.getName());
        this.setPhone(other.getPhone());
        this.setMobile(other.getMobile());
        this.setEmail(other.getEmail());

        // 即时通讯信息
        this.setWechat(other.getWechat());
        this.setQQ(other.getQQ());

        // 工作信息
        this.setCompany(other.getCompany());
        this.setHomeAddress(other.getHomeAddress());
        this.setPostalCode(other.getPostalCode());

        // 个人资料
        this.setBirthday(other.getBirthday()); // LocalDate类型直接赋值
        this.setRemark(other.getRemark());
        this.setStarred(other.isStarred());
        this.setPhotoPath(other.getPhotoPath());

        // 分组信息（重要！保持列表的Observable特性）
        this.getGroups().setAll(other.getGroups());

        // 时间戳信息（根据需求决定是否同步）
        // this.setCreatedAt(other.getCreatedAt());
        // this.setUpdatedAt(LocalDateTime.now());
    }

    public Map<String, String> getImAccounts() {
        return Map.of(
                "微信", getWechat(),
                "QQ", getQQ()
        );
    }

    // 新增方法：批量设置账号（根据平台名更新对应属性）
    public void setImAccount(String platform, String account) {
        switch (platform) {
            case "微信" -> setWechat(account);
            case "QQ" -> setQQ(account);
            default -> throw new IllegalArgumentException("不支持的平台：" + platform);
        }
    }
}